﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.AuthorityManageForms.RoleForms
{
    public partial class RoleDetailForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.RoleBll roleBll = new BLL.RoleBll();
        Action _InitGrid;
        int _option;
        Role _role;
        public RoleDetailForm(Action InitGrid)
        {
            InitializeComponent();
            _InitGrid = InitGrid;
            _option = 1;
        }

        public RoleDetailForm(Role role, Action InitGrid)
        {
            InitializeComponent();
            _option = 2;
            _role = role;
            _InitGrid = InitGrid;
        }

        private void RoleDetailForm_Load(object sender, EventArgs e)
        {

            if (_option == 1)
            {
                this.Text = ResourceOverall.CreateStr;
            }
            if (_option == 2)
            {
                this.Text = ResourceOverall.EditStr;
                this.textEdit_RoleName.Text = _role.RoleName;
            }

        }

        private void simpleButton_save_Click(object sender, EventArgs e)
        {
            using (OJCB4Entities db = new OJCB4Entities())
            {
                if (_option == 1)
                {
                    int count = roleBll.LoadEntities(m => m.RoleName == this.textEdit_RoleName.Text).Count();
                    if (count > 0)
                    {
                        XtraMessageBox.Show(ResourceOverall.DataExists);
                        return;
                    }
                    Role role = new Role();
                    role.RoleID = Guid.NewGuid().ToString();
                    role.RoleName = this.textEdit_RoleName.Text;
                    db.Roles.Add(role);
                    bool res= db.SaveChanges() > 0;
                    if (res)
                    {
                        _InitGrid();
                        XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                        this.Close();
                    }
                }
                else if (_option == 2)
                {
                    Role role = db.Roles.Find(_role.RoleID);
                    role.RoleName = this.textEdit_RoleName.Text;
                    bool res = db.SaveChanges()>0;
                    if (res)
                    {
                        _InitGrid();
                        XtraMessageBox.Show(ResourceOverall.EditSucceed);
                        this.Close();
                    }
                }
            }
        }
    }
}